package com.dofun.locationservice.base;

import android.app.Activity;
import android.os.Bundle;

import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;


/**
 * BaseAppCompatActivity
 * 备注:
 * 1.XXActivity 继承 MvpActivity,当页面存在 Presenter 时，具体 Activity 需要调用 setPresenter(P... presenter)
 * 2.由于此框架集合了 RxLifecycle 因此本 Activity 继承自 RxActivity (开发者也可以直接继承 Activity)
 * 3.支持一个 Activity 存在多个 Presenter
 *
 * @param <V>
 * @param <P>
 */
public abstract class BaseAppCompatActivity<V extends IBaseView, P extends IBasePresenter<V>>
        extends RxAppCompatActivity implements IBaseView {

    /**
     * Activity
     */
    protected Activity activity;
    /**
     * V & P
     */
//    private IDelegateCallback<V, P> delegateCallback;

    private P[] pArray;

    /**
     * 获取 Presenter 数组
     */
    protected abstract P[] getPresenterArray();

    public P[] getPresenter() {
        return getPresenterArray();
    }

    public V[] getMvpView() {
        V[] view = null;
        if (pArray != null) {
            view = (V[]) new IBaseView[pArray.length];
            for (int i = 0; i < pArray.length; i++) {
                view[i] = (V) this;
            }
        }
        return view;
    }


    /**
     * 是否保留V&P实例
     *
     * @return
     */
    private static boolean retainVPInstance(Activity activity) {
        return activity.isChangingConfigurations() || !activity.isFinishing();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = this;
//        delegateCallback = this;
        pArray = getPresenter();
        if (pArray != null) {
            V[] vArray = getMvpView();
            P presenter;
            V view;
            for (int i = 0; i < pArray.length; i++) {
                presenter = pArray[i];
                view = vArray[i];
                if (presenter != null && view != null) {
                    //关联view
                    presenter.attachView(view);
                }
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        pArray = getPresenter();
        if (pArray != null) {
            P presenter;
            for (int i = 0; i < pArray.length; i++) {
                presenter = pArray[i];
                if (presenter != null) {
                    //解除View
                    presenter.detachView();
                    if (!retainVPInstance(activity)) {
                        //销毁 V & P 实例
                        presenter.destroy();
                    }
                }
            }
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    public void onContentChanged() {
        super.onContentChanged();
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
    }

}
